---
--- 按键控制
---
---@class Battle.KeyMgr
local KeyMgr = Battle.CreateClass("KeyMgr")
Battle.KeyMgr = KeyMgr
local this = KeyMgr

local HERO_MASK = 2^LayerMask.NameToLayer("Hero")
local SCENE_MASK = 2^LayerMask.NameToLayer("Scene")

function KeyMgr.OnCreate()
    ---@type Battle.Entity
    this.ctrlActor = nil
end
function KeyMgr.OnDestroy()
    this.ctrlActor = nil
end

function KeyMgr.TouchBegin()
    if not Battle.ClientBattle then return end

    this.ctrlActor = nil
    local flag,hit = Util.Raycast(Battle.SceneMgr.camera,nil,HERO_MASK)
    if flag then
        local hitActor = Battle.ActorManager.GetClickActor(hit.transform)
        if hitActor  then
            if hitActor.camp == Battle.CampType.Blue then
                this.ctrlActor = hitActor
            end
        end
    end
    if this.ctrlActor == nil then return end

    if this.ctrlActor.actorType == Battle.ActorType.Lord then
        if not this.ctrlActor.skillSystem:GetSkill1():CanRelease() then
            this.ctrlActor = nil
        end
        return
    end
    --if this.ctrlActor.effectStateSystem:CanMove() then
        local flag,x,y,z = Battle.SceneMgr.GetRaycastScenePos()
        if flag then
            this.ctrlActor.eventSystem:FireEvent(Battle.Event.ActorHandMove,Battle.EventArgs.ActorHandMoveArgs:Create(this.ctrlActor,x,y,z))
        end
    --end

end
function KeyMgr.TouchMove()
    if not Battle.ClientBattle then return end
    if this.ctrlActor == nil then return end

    if this.ctrlActor.actorType == Battle.ActorType.Lord then
        this.ctrlActor.eventSystem:FireEvent(Battle.Event.SkillReleaseUpdatePos,Battle.EventArgs.SkillArgs:Create(this.ctrlActor.skillSystem:GetSkill1()))
        return
    end

    --if this.ctrlActor.effectStateSystem:CanMove() then
        local flag,x,y,z = Battle.SceneMgr.GetRaycastScenePos()
        if flag then
            this.ctrlActor.eventSystem:FireEvent(Battle.Event.ActorHandMove,Battle.EventArgs.ActorHandMoveArgs:Create(this.ctrlActor,x,y,z))
        end
    --end
end
function KeyMgr.TouchEnd()
    if not Battle.ClientBattle then return end
    if this.ctrlActor == nil then return end

    if this.ctrlActor.actorType == Battle.ActorType.Lord then
        this.ctrlActor.eventSystem:FireEvent(Battle.Event.SkillEmit,Battle.EventArgs.SkillArgs:Create(this.ctrlActor.skillSystem:GetSkill1()))
        this.ctrlActor = nil
        return
    end
    local flag,x,y,z = Battle.SceneMgr.GetRaycastScenePos()
    if flag then
        this.ctrlActor.eventSystem:FireEvent(Battle.Event.ActorHandMoveEnd,Battle.EventArgs.ActorHandMoveArgs:Create(this.ctrlActor,x,y,z))
    end
    local flag,hit = Util.Raycast(Battle.SceneMgr.camera,nil,SCENE_MASK)
    if flag then
        if Battle.PlayerSystem.GetMyPlayer():ReduceAction(this.ctrlActor) then
            this.ctrlActor.aiSystem:SetBlackboardValue("hand_move",true)
            this.ctrlActor.aiSystem:SetBlackboardValue("hand_pos",hit.point)
        else
            logError("行动点不足")
            if this.ctrlActor.aiSystem:GetBlackboardValue("hand_move") == true then
                local hand_pos = this.ctrlActor.aiSystem:GetBlackboardValue("hand_pos")
                this.ctrlActor.eventSystem:FireEvent(Battle.Event.ActorHandMoveEnd,Battle.EventArgs.ActorHandMoveArgs:Create(this.ctrlActor,hand_pos.x,hand_pos.y,hand_pos.z))
            end
        end
    end
    if this.ctrlActor.effectStateSystem:CanMove() then
        this.ctrlActor.aiSystem:SetBlackboardValue("hand_skill",false)
    end
    this.ctrlActor = nil
end


